<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '120px' : '140px'">
        <el-col :span="12">
          <!-- 队列编码 -->
          <el-form-item label="队列编码"
                        prop="queueName">
            <!--文本框-->
            <el-input v-model="dataForm.queueName" :disabled="disable"
                      placeholder="队列编码" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 队列描述 -->
          <el-form-item label="队列描述"
                        prop="queueDesc">
            <!--文本框-->
            <el-input v-model="dataForm.queueDesc"
                      placeholder="队列描述" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 队列最大值 -->
          <el-form-item label="队列最大值"
                        prop="queueSize">
            <!--计数器-->
            <el-select v-model="dataForm.queueSize" placeholder="请选择" clearable>
              <el-option
                v-for="item in queueSizeList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 消费者最大数量 -->
          <el-form-item label="消费者数量"
                        prop="consumerNum">
            <!--计数器-->
            <el-input-number v-model="dataForm.consumerNum" :min="4" :max="10"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 消费者类 -->
          <el-form-item label="消费者类"
                        prop="consumerClass">
            <!--文本框-->
            <el-input v-model="dataForm.consumerClass"
                      placeholder="消费者类" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 队列等待时休眠时间（毫秒） -->
          <el-form-item label="休眠时间（毫秒）"
                        prop="sleepMillisecond">
            <!--计数器-->
            <el-select v-model="dataForm.sleepMillisecond" placeholder="请选择" clearable>
              <el-option
                v-for="item in sleepMillisecondList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 休眠最大间隔，超过间隔线程退出 -->
          <el-form-item label="休眠最大间隔"
                        prop="sleepMaxInterval">
            <!--计数器-->
            <el-input-number v-model="dataForm.sleepMaxInterval" :min="10"
                             :step="10" step-strictly :max="100"></el-input-number>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      disable: false,
      dataForm: {
        id: '',
        queueName: '',
        queueDesc: '',
        queueSize: '',
        consumerClass: '',
        consumerNum: '',
        sleepMillisecond: '',
        sleepMaxInterval: '',
        status: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        queueName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { min: 5, max: 36, message: '长度在 3 到 36 个字符', trigger: 'blur' }
        ],
        queueDesc: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { min: 3, max: 36, message: '长度在 3 到 36 个字符', trigger: 'blur' }
        ],
        queueSize: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        consumerClass: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        consumerNum: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sleepMillisecond: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sleepMaxInterval: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    },
    queueSizeList () {
      return [{ 'value': '1000', 'label': '1000' },
        { 'value': '2000', 'label': '2000' },
        { 'value': '5000', 'label': '5000' },
        { 'value': '10000', 'label': '10000' }]
    },
    sleepMillisecondList () {
      return [{ 'value': '1000', 'label': '1000' },
        { 'value': '2000', 'label': '2000' },
        { 'value': '5000', 'label': '5000' },
        { 'value': '10000', 'label': '10000' }]
    }
  },
  methods: {
    init () {
      this.visible = true
      this.disable = false
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/queue/apiQueueConf/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        this.disable = true
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/queue/apiQueueConf/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
